package com.sgb.member.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.R;
import com.sgb.member.databinding.MemberTypeItemBinding;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择会员类型适配器
 */
public class MemberTypeAdapter extends RecyclerView.Adapter<MemberTypeAdapter.ViewHolder> {
    private Context context;
    private List<CommonPopEntity> datas;
    private final LayoutInflater inflater;
    private int select = 0;
    private Callback callback;

    public void setSelect(int p){
        select = p;
    }
    public void setTypeCallback(Callback callback) {
        this.callback = callback;
    }

    public MemberTypeAdapter(Context context, List<CommonPopEntity> list) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        datas = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MemberTypeItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.member_type_item, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        CommonPopEntity bean = datas.get(position);
        holder.getBinding().tvName.setText(bean.getTypeName());
        holder.getBinding().tvName.setOnClickListener(v -> {
            select = position;
            callback.back(datas.get(position).getChildren()==null?new ArrayList<>():datas.get(position).getChildren(),position);
            notifyDataSetChanged();
        });
        if(select==position){
            holder.getBinding().tvName.setBackgroundResource(R.drawable.shape_check_txt);
            holder.getBinding().tvName.setTextColor(context.getResources().getColor(R.color.color_EF4033));
        }else {
            holder.getBinding().tvName.setBackgroundResource(R.drawable.shape_uncheck_txt);
            holder.getBinding().tvName.setTextColor(context.getResources().getColor(R.color.color_tab_two));
        }
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private MemberTypeItemBinding mBinding;

        public MemberTypeItemBinding getBinding() {
            return mBinding;
        }

        public ViewHolder(MemberTypeItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }

    public interface Callback {
        void back(List<CommonPopEntity> datas,int p);
    }
}
